package top.rainf.service.impl;


import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.rainf.domain.vo.Notice;
import top.rainf.domain.vo.Post;
import top.rainf.mapper.NoticeMapper;
import top.rainf.service.INoticeService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 星环
 * @date Wed May 10 21:04:41 CST 2023
 * @description Notice服务的实现类
 */
@Service
public class NoticeService implements INoticeService {

    @Autowired
    NoticeMapper noticeMapper;

    @Override
    public List<Notice> getNoticeList() {
        return noticeMapper.selectAll();
    }



    @Override
    public Notice getOneById(int id) {
        return noticeMapper.selectById(id);
    }

    @Override
    public boolean deleteOneById(int id) {
        return noticeMapper.deleteById(id) == 1;
    }

    @Override
    public boolean deleteAll(List<Integer> idList) {
        return noticeMapper.deleteByIds(idList) == idList.size();
    }

    @Override
    public boolean updateById(Notice notice) {
        return noticeMapper.updateById(notice) == 1;
    }

    @Override
    public void sendTo(int authorId, String s) {
        noticeMapper.insertOne(authorId,s);
    }

    @Override
    public List<Notice> getUserSimplePostListPaged(int userId, int nowPage, int numPerPage) {
        int start = (nowPage - 1) * numPerPage;
        List<Notice> noticeList = noticeMapper.selectUserSimpleNoticeListPaged(userId, start, numPerPage);
        List<Integer> ids = new ArrayList<>();
        noticeList.forEach(notice -> ids.add(notice.getId()));
        if(ids.size() <= 0){
            return noticeList;
        }
        System.out.println("ids is " + ids);
        noticeMapper.updateCheckByIds(ids,1);
        return noticeList;
    }

    @Override
    public int getTotalCountOfUser(int userId) {
        return noticeMapper.selectCountOfUserNotice(userId);
    }

    @Override
    public int checkNew(int id) {
        return noticeMapper.selectUserNewNoticeCount(id);
    }
}
